package js_new_sms.js_new_sms;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.nl.util.Common;
import com.nl.util.ConnectionManager;

public class App 
{
	private static final Logger logger =Logger.getLogger(App.class);
	
    public static void main( String[] args )
    {	
    	Connection conn = null;
		PreparedStatement pres = null;
		PreparedStatement pres2 = null;
		ResultSet rs1 = null;
		ResultSet rs2 = null;
		int count = 0;
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss");
		String now = sdf1.format(date);
		String nowDay=now.substring(0, 8);
		String nowTime = sdf.format(date).substring(11);
		StringBuffer sBuffer = new StringBuffer();
    	String sql1 = "select a.id,b.everyday_start_time,b.everyday_end_time,to_char(b.start_date,'yyyymmddhh24miss') startDate,to_char(b.end_date,'yyyymmddhh24miss') endDate,b.real_time realTime from sms_mgr_info a,sms_template_attr b where a.id=b.id and a.state =0 and a.IS_ARREARAGE = 0 and a.open_account=1 and  b.state=2 and a.id !=1000001839";
    	String sql2 = "select t.mgr_id,count(*) c from boss_sm_send t where t.create_date > trunc(sysdate) and t.mgr_id = ?  group by t.mgr_id";
    	try {
    		conn = ConnectionManager.getInstance().getConnection();
			pres = conn.prepareStatement(sql1);
			rs1 = pres.executeQuery();
			while(rs1.next()){
				count = 0;
				String mgrId = rs1.getString("id");
				String startDate = rs1.getString("startDate");
				
				
				
				String everyDayStartTime = rs1.getString("everyday_start_time");
				String everyDayEndTime = rs1.getString("everyday_end_time");
				String realTime = rs1.getString("realTime");
				int delayTime=Integer.valueOf(realTime);
				Date start=ft.parse(everyDayStartTime);
				Date realStartTime = new Date(start.getTime() + delayTime*60*1000);
				everyDayStartTime=ft.format(realStartTime);
				if (everyDayStartTime.compareTo(nowTime)>= 0 ) {
					continue;
				}
				
				if(everyDayEndTime.compareTo(nowTime)<=0){
					continue;
				}
				
				if (startDate.compareTo(now)>= 0 ) {
					continue;
				}
				String endDate = rs1.getString("endDate");
				if(""!=endDate&&null!=endDate){
					String endDay=endDate.substring(0,8);
		
					Date endDate2=sdf1.parse(endDate);
					
					endDate=sdf.format(endDate2);
					
					String endTime=endDate.substring(11);
					//去除项目结束当天，并且每天开始发送时间大于项目结束时间点。
					if(nowDay.equals(endDay)&&(everyDayStartTime.compareTo(endTime)>0)){
						continue;
					}
				}
				
				
				pres2 = conn.prepareStatement(sql2);
				pres2.setString(1, mgrId);
				rs2 = pres2.executeQuery();
				while (rs2.next()) {
					count ++;
					String id = rs2.getString("mgr_id");
					int sum = rs2.getInt("c");
					if (sum > 0  || 0 == sum ) {
						logger.info("项目:"+id+",发送量:"+sum+";");
					}
				}
				if (count == 0) {
					sBuffer.append("项目:"+mgrId+",无发送量;");
				}
			}
			if (sBuffer.toString().length() == 0) {
				sBuffer.append("项目发送量正常，暂未发现无发送量项目。");
			}
			Common.sendWarnSms(sBuffer.toString());
			logger.info(sBuffer.toString());
		} catch (Exception e) {
			logger.error(e);
		}
    }
   
}
